<?php include("IncAuthentication.php")?>

<?php
if(isset($_POST['btnPurchase']))
{
	// tao hoa don
	$MaKH = $_SESSION['KhachHang']['MaKH'];
	$NgayDat = date("Y-m-d");
	$NgayCan = $_POST['txtNgayCan'];
	$HoTen = $_POST['txtHoTen'];
	$DiaChi = $_POST['txtDiaChi'];
	$CachThanhToan = $_POST['cboCachThanhToan'];
	$CachVanChuyen = $_POST['cboCachVanChuyen'];
	$PhiVanChuyen = $_POST['txtPhiVanChuyen'];
	$GhiChu = $_POST['txtGhiChu'];
	
	$sql = "INSERT INTO HoaDon(MaKH, NgayDat, NgayCan, HoTen, DiaChi, 
			CachThanhToan, CachVanChuyen, PhiVanChuyen, GhiChu) VALUES('$MaKH', 
			'$NgayDat', '$NgayCan', '$HoTen', '$DiaChi', '$CachThanhToan', 
			'$CachVanChuyen', '$PhiVanChuyen', '$GhiChu')";
	mysql_query($sql) or die(mysql_error());
	
	// lay ma tu tang cua hoa don vua sinh ra
	$MaHD = mysql_insert_id();

	// tao chi tiet hoa don
	$sql = "INSERT INTO ChiTietHD(MaHD, MaHH, DonGia, GiamGia, SoLuong) VALUES";
	$GioHang = $_SESSION['GioHang'];    
	foreach($GioHang as $MaHH=>$MatHang)
	{
		$MaHH = $MatHang['MaHH'];
		$DonGia = $MatHang['DonGia'];
		$GiamGia = $MatHang['GiamGia'];
		$SoLuong = $MatHang['SoLuong'];
		
		$sql = $sql . "('$MaHD', '$MaHH', '$DonGia', '$GiamGia', '$SoLuong'),";
	}
	$sql = substr($sql, 0, strlen($sql)-1);
	mysql_query($sql) or die(mysql_error());

	// xoa gio hang
	$_SESSION['GioHang'] = array();
	
	// chuyen ve trang PurchasedOrders.php de xem danh sach don hang da dat
	header("Location: index.php?p=PurchasedOrders");
}
?>
<form method="post">
  <table width="97%" border="0" cellpadding="5" cellspacing="0" style="text-align:left; border-color:#F60"">
    <tr>
      <td><table border="0" align="center" cellpadding="5" cellspacing="0">
        <tr>
          <td>Customer</td>
          <td>:</td>
          <td><?php echo $_SESSION['KhachHang']['HoTen'] ?></td>
        </tr>
        <tr>
          <td>Order Date</td>
          <td>:</td>
          <td><?php echo date("F d, Y")?>&nbsp;</td>
        </tr>
        <tr>
          <td>Required Date</td>
          <td>:</td>
          <td><input name="txtNgayCan" type="text" id="txtNgayCan" size="10" />
            (Year-Month-Day)</td>
        </tr>
        <tr>
          <td>Recipient's Name</td>
          <td>:</td>
          <td><input name="txtHoTen" type="text" id="txtHoTen" size="44" value="<?php echo $_SESSION['KhachHang']['HoTen'] ?>" /></td>
        </tr>
        <tr>
          <td>Recipient's Address</td>
          <td>:</td>
          <td><input name="txtDiaChi" type="text" id="txtDiaChi" size="44" value="<?php echo $_SESSION['KhachHang']['DiaChi'] ?>"/></td>
        </tr>
        <tr>
          <td>Payment Method</td>
          <td>:</td>
          <td><select name="cboCachThanhToan" id="cboCachThanhToan">
            <option value="Cash">Cash</option>
            <option value="Cheque">Cheque</option>
            <option value="Credit Card">Credit Card</option>
          </select></td>
        </tr>
        <tr>
          <td><p>Shipping Method</p></td>
          <td>:</td>
          <td><select name="cboCachVanChuyen" id="cboCachVanChuyen">
            <option>Airway</option>
            <option>Land</option>
            <option>Seaway</option>
          </select></td>
        </tr>
        <tr>
          <td>Freight</td>
          <td>:</td>
          <td><input name="txtPhiVanChuyen" type="text" id="txtPhiVanChuyen" value="0" size="10" /></td>
        </tr>
        <tr>
          <td valign="top">Notes</td>
          <td valign="top">:</td>
          <td><textarea name="txtGhiChu" cols="49" rows="5" id="txtGhiChu"></textarea></td>
        </tr>
      </table></td>
    </tr>
    <tr>
      <td>


           <table cellpadding="5" cellspacing="0" border="1" bordercolor="#FFFFFF" class="art-article" style="width:98%">
		<tr style="background-color:#FC6; color:#900">
				  <th>Id</th>
                  <th>Name</th>
                  <th>Price</th>
                  <th>Disc</th>
                  <th>Qty</th>
                    <th>Amt</th>
                </tr>
<?php
	$GioHang = $_SESSION['GioHang'];    
	$TongTien = 0;
	foreach($GioHang as $MaHH=>$MatHang)
	{
		$MaHH = $MatHang['MaHH'];
		$TenHH = $MatHang['TenHH'];
		$DonGia = $MatHang['DonGia'];
		$GiamGia = $MatHang['GiamGia']*100;
		$SoLuong = $MatHang['SoLuong'];
		$ThanhTien = $DonGia*$SoLuong*(1-$GiamGia);
		$TongTien += $ThanhTien;
		echo "
		<tr>
			<td>$MaHH</td>
			<td>$TenHH</td>
			<td>$$DonGia</td>
			<td>$GiamGia%</td>
			<td>$SoLuong</td>
			<td>$$ThanhTien</td>
		</tr>
		";
	}
?>
              <tr>
                    <th colspan="5" style="text-align:right">Total amount: </th>
                    <th align="left">$<?php echo $TongTien?></th>
                </tr>
            </table>

      </td>
    </tr>
    <tr>
      <td align="center"><input class="art-button" type="submit" name="btnPurchase" id="btnPurchase" value="Purchase" /></td>
    </tr>
  </table>
</form>